/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package utn.tsb.tej.front_end.Maps;

import com.trolltech.qt.core.QPointF;
import com.trolltech.qt.core.QRectF;
import com.trolltech.qt.gui.QColor;
import com.trolltech.qt.gui.QGraphicsItem;
import com.trolltech.qt.gui.QPainter;
import com.trolltech.qt.gui.QPixmap;
import com.trolltech.qt.gui.QStyleOptionGraphicsItem;
import com.trolltech.qt.gui.QWidget;
import utn.tsb.tej.world_logic.Color;

/**
 *
 * @author Carlos S. Moreno <CarlosS.Moreno@gmail.com>
 */
public class Chip extends QGraphicsItem{

    private Color color;
    private int chipsCount,missilesCount, X, Y, width, height;
    private QPixmap chips, missile;
    
    public Chip(Color color, int chipsCount, int missilesCount, int X, int Y)
    {
        super();
        this.color = color;

        this.chipsCount = chipsCount;
        this.missilesCount = missilesCount;

        try{
        chips = new QPixmap(getImagePath());

        missile = new QPixmap(getClass().getResource(java.io.File.separator +
                "images"+ java.io.File.separator + "Missile.png").getPath());
        }
        catch (NullPointerException e)
        {
            System.out.println("EXEC: Chip and/or Missile image not found!");
        }
        
        width = chips.width();
        height = chips.height();
        this.X = X;
        this.Y = Y;
        //this.setPixmap(image);


        this.setPos(X, Y);
        this.setPos(this.pos().add(new QPointF(-(width/2), -(height/2))));

    }

    public Chip(Color color, int chipsCount, int X, int Y)
    {
        this (color, chipsCount, 0, X, Y);
    }

    public Chip(Color color, int chipsCount)
    {
        this(color, chipsCount, 0, 0, 0);
    }

    public Chip ()
    {
       this(new Color(), 1, 0, 0 ,0);
    }



    private String getImagePath()
    {
        int number = 1;
        if (chipsCount > 0 && chipsCount < 5) {
            number = chipsCount;
        } else if (chipsCount > 4){
            number = 4;
        }

        String osBar = java.io.File.separator;
        String fileName = "";

        if (color.getCodeRGB().compareToIgnoreCase(Color.BLACK) == 0) {
            fileName = "Black_Ball";
        } else if (color.getCodeRGB().compareToIgnoreCase(Color.BLUE) == 0) {
            fileName = "Blue_Ball";
        } else if (color.getCodeRGB().compareToIgnoreCase(Color.GREEN) == 0) {
            fileName = "Green_Ball";
        } else if (color.getCodeRGB().compareToIgnoreCase(Color.RED) == 0) {
            fileName = "Red_Ball";
        } else if (color.getCodeRGB().compareToIgnoreCase(Color.WHITE) == 0) {
            fileName = "Fuccia_Ball";
        } else {
            fileName = "Yellow_Ball";
        }

        System.out.println("fileName="+fileName+chipsCount+".png");
        return getClass().getResource(osBar + "images"+
                osBar + fileName + number + ".png").getPath();
    }

    public void addArmies(int amount)
    {
        chipsCount += amount;
        updateChip();
    }

    public void removeArmies(int amount)
    {
        chipsCount -= amount;
        updateChip();
    }

    public void addMissiles(int amount)
    {
        missilesCount += amount;
    }

    public void removeMissiles(int amount)
    {
        missilesCount -= amount;
    }

    private void updateChip()
    {
        chips = new QPixmap(getImagePath());
    }

    @Override
    public QRectF boundingRect() {
        double penWidth = 1;
        return new QRectF(-width/2 - penWidth / 2, -width/2 - penWidth / 2,
                       width + penWidth, width + penWidth);
    }

    @Override
    public void paint(QPainter qp, QStyleOptionGraphicsItem qsogi, QWidget qw) {
        qp.drawPixmap(new QPointF( - 16d,  - 16d), chips);
        if (chipsCount > 4 && chipsCount <= 9) {
            qp.drawText(new QPointF( - 5d, 0), String.valueOf(chipsCount));
        } else if (chipsCount > 9) {
            qp.drawText(new QPointF( - 8d, 0), String.valueOf(chipsCount));
        }
        if (missilesCount > 0) {
            qp.drawPixmap(new QPointF(0, 0), missile);
            qp.setPen(QColor.white);
            qp.drawText(new QPointF( 4d, 13d), String.valueOf(missilesCount));
            qp.setPen(QColor.black);
        }

    }

}
